package org.jeecg.modules.product.risk.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.system.query.QueryGenerator;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.product.risk.entity.TbBaseCheckPlan;
import org.jeecg.modules.product.risk.entity.TbBaseCheckPlanApprovalRecord;
import org.jeecg.modules.product.risk.entity.TbBaseCheckRecord;
import org.jeecg.modules.product.risk.service.ITbBaseCheckPlanApprovalRecordService;
import org.jeecg.modules.product.risk.service.ITbBaseCheckPlanService;
import org.jeecg.modules.product.risk.service.ITbBaseCheckRecordService;
import org.jeecg.modules.product.risk.vo.TbBaseCheckPlanPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import java.util.Arrays;
import org.jeecg.common.util.oConvertUtils;

import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

 /**
 * @Description: 检查计划
 * @Author: jeecg-boot
 * @Date:   2024-05-17
 * @Version: V1.0
 */
@Tag(name="检查计划")
@RestController
@RequestMapping("/TbBaseCheckPlan/tbBaseCheckPlan")
@Slf4j
public class TbBaseCheckPlanController extends JeecgController<TbBaseCheckPlan, ITbBaseCheckPlanService> {

	@Autowired
	private ITbBaseCheckPlanService tbBaseCheckPlanService;

	@Autowired
	private ITbBaseCheckRecordService tbBaseCheckRecordService;

	@Autowired
	private ITbBaseCheckPlanApprovalRecordService tbBaseCheckPlanApprovalRecordService;


	/*---------------------------------主表处理-begin-------------------------------------*/

	/**
	 * 分页列表查询
	 * @param tbBaseCheckPlan
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "检查计划-分页列表查询")
	@Operation(summary="检查计划-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(TbBaseCheckPlan tbBaseCheckPlan,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<TbBaseCheckPlan> queryWrapper = QueryGenerator.initQueryWrapper(tbBaseCheckPlan, req.getParameterMap());
		Page<TbBaseCheckPlan> page = new Page<TbBaseCheckPlan>(pageNo, pageSize);
		IPage<TbBaseCheckPlan> pageList = tbBaseCheckPlanService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	/**
     *   添加
     * @param tbBaseCheckPlan
     * @return
     */
    @AutoLog(value = "检查计划-添加")
    @Operation(summary="检查计划-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody TbBaseCheckPlan tbBaseCheckPlan) {
		tbBaseCheckPlan.setRegistrationTime(DateUtils.now());
        tbBaseCheckPlanService.save(tbBaseCheckPlan);
        return Result.OK("添加成功！");
    }

    /**
     *  编辑
     * @param tbBaseCheckPlan
     * @return
     */
    @AutoLog(value = "检查计划-编辑")
    @Operation(summary="检查计划-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody TbBaseCheckPlan tbBaseCheckPlan) {
        tbBaseCheckPlanService.updateById(tbBaseCheckPlan);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     * @param id
     * @return
     */
    @AutoLog(value = "检查计划-通过id删除")
    @Operation(summary="检查计划-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name="id",required=true) String id) {
        tbBaseCheckPlanService.delMain(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @AutoLog(value = "检查计划-批量删除")
    @Operation(summary="检查计划-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
        this.tbBaseCheckPlanService.delBatchMain(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }
	 /**
	  *   通过id逻辑删除
	  *
	  * @param id
	  * @return
	  */
	 @AutoLog(value = "检查计划-通过id逻辑删除")
	 @Operation(summary="检查计划-通过id逻辑删除")
	 @DeleteMapping(value = "/logicDelete")
	 public Result<?> logicDelete(@RequestParam(name="id",required=true) String id) {
		 TbBaseCheckPlan tbBaseCheckPlan = tbBaseCheckPlanService.getById(id);
		 tbBaseCheckPlan.setDeleted("1");//禁止状态1是已删除，0是正常未删除
		 tbBaseCheckPlanService.updateById(tbBaseCheckPlan);
		 return Result.OK("删除成功!");
	 }

	 /**
	  *  批量逻辑删除
	  *
	  * @param ids
	  * @return
	  */
	 @AutoLog(value = "检查计划-批量逻辑删除")
	 @Operation(summary="检查计划-批量逻辑删除")
	 @DeleteMapping(value = "/deleteLogicBatch")
	 public Result<?> deleteLogicBatch(@RequestParam(name="ids",required=true) String ids) {
		 List<TbBaseCheckPlan> list = tbBaseCheckPlanService.listByIds(Arrays.asList(ids.split(",")));
		 for (TbBaseCheckPlan tbBaseCheckPlan : list) {
			 tbBaseCheckPlan.setDeleted("1");//禁止状态1是已删除，0是正常未删除
		 }
		 tbBaseCheckPlanService.updateBatchById(list);
		 return Result.OK("批量删除成功！");
	 }

	 /**
	  * 获取被逻辑删除的检查计划
	  *
	  * @return logicDeletedUserList
	  */
	 @GetMapping("/recycleBin")
	 public Result getRecycleBin(@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								 @RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {
		 QueryWrapper<TbBaseCheckPlan> queryWrapper = new QueryWrapper<>();
		 queryWrapper.eq("deleted", "1");//查询已经删除的数据
		 Page<TbBaseCheckPlan> page = new Page<TbBaseCheckPlan>(pageNo, pageSize);
		 IPage<TbBaseCheckPlan> pageList = tbBaseCheckPlanService.page(page, queryWrapper);
		 return Result.ok(pageList);
	 }

	 /**
	  * 还原被逻辑删除的检查计划
	  *
	  * @param jsonObject
	  * @return
	  */
	 @RequestMapping(value = "/putRecycleBin", method = RequestMethod.PUT)
	 public Result putRecycleBin(@RequestBody JSONObject jsonObject, HttpServletRequest request) {
		 String tbBaseCheckPlanIds = jsonObject.getString("tbBaseCheckPlanIds");
		 if (StringUtils.isNotBlank(tbBaseCheckPlanIds)) {
			 List<TbBaseCheckPlan> list = tbBaseCheckPlanService.listByIds(Arrays.asList(tbBaseCheckPlanIds.split(",")));
			 for (TbBaseCheckPlan entity : list) {
				 entity.setDeleted("0");
			 }
			 tbBaseCheckPlanService.updateBatchById(list);
		 }
		 return Result.ok("还原成功");
	 }
    /**
     * 导出
     * @return
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, TbBaseCheckPlan tbBaseCheckPlan) {
        return super.exportXls(request, tbBaseCheckPlan, TbBaseCheckPlan.class, "检查计划");
    }

    /**
     * 导入
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, TbBaseCheckPlan.class);
    }
	/*---------------------------------主表处理-end-------------------------------------*/


    /*--------------------------------子表处理-检查情况记录-begin----------------------------------------------*/
	/**
	 * 通过主表ID查询
	 * @return
	 */
	@AutoLog(value = "检查情况记录-通过主表ID查询")
	@Operation(summary="检查情况记录-通过主表ID查询")
	@GetMapping(value = "/listTbBaseCheckRecordByMainId")
    public Result<?> listTbBaseCheckRecordByMainId(TbBaseCheckRecord tbBaseCheckRecord,
												   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
												   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
												   HttpServletRequest req) {
        QueryWrapper<TbBaseCheckRecord> queryWrapper = QueryGenerator.initQueryWrapper(tbBaseCheckRecord, req.getParameterMap());
        Page<TbBaseCheckRecord> page = new Page<TbBaseCheckRecord>(pageNo, pageSize);
		queryWrapper.eq("deleted","0");
        IPage<TbBaseCheckRecord> pageList = tbBaseCheckRecordService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

	/**
	 * 添加
	 * @param tbBaseCheckRecord
	 * @return
	 */
	@AutoLog(value = "检查情况记录-添加")
	@Operation(summary="检查情况记录-添加")
	@PostMapping(value = "/addTbBaseCheckRecord")
	public Result<?> addTbBaseCheckRecord(@RequestBody TbBaseCheckRecord tbBaseCheckRecord) {
		tbBaseCheckRecordService.save(tbBaseCheckRecord);
		return Result.OK("添加成功！");
	}

    /**
	 * 编辑
	 * @param tbBaseCheckRecord
	 * @return
	 */
	@AutoLog(value = "检查情况记录-编辑")
	@Operation(summary="检查情况记录-编辑")
	@PutMapping(value = "/editTbBaseCheckRecord")
	public Result<?> editTbBaseCheckRecord(@RequestBody TbBaseCheckRecord tbBaseCheckRecord) {
		tbBaseCheckRecordService.updateById(tbBaseCheckRecord);
		return Result.OK("编辑成功!");
	}

	/**
	 * 通过id删除
	 * @param id
	 * @return
	 */
	@AutoLog(value = "检查情况记录-通过id删除")
	@Operation(summary="检查情况记录-通过id删除")
	@DeleteMapping(value = "/deleteTbBaseCheckRecord")
	public Result<?> deleteTbBaseCheckRecord(@RequestParam(name="id",required=true) String id) {
		tbBaseCheckRecordService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 * 批量删除
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "检查情况记录-批量删除")
	@Operation(summary="检查情况记录-批量删除")
	@DeleteMapping(value = "/deleteBatchTbBaseCheckRecord")
	public Result<?> deleteBatchTbBaseCheckRecord(@RequestParam(name="ids",required=true) String ids) {
	    this.tbBaseCheckRecordService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	 /**
	  *   通过id逻辑删除
	  *
	  * @param id
	  * @return
	  */
	 @AutoLog(value = "检查计划-通过id逻辑删除")
	 @Operation(summary="检查计划-通过id逻辑删除")
	 @DeleteMapping(value = "/logicDeleteTbBaseCheckRecord")
	 public Result<?> logicDeleteTbBaseCheckRecord(@RequestParam(name="id",required=true) String id) {
		 TbBaseCheckRecord tbBaseCheckRecord = tbBaseCheckRecordService.getById(id);
		 tbBaseCheckRecord.setDeleted("1");//禁止状态1是已删除，0是正常未删除
		 tbBaseCheckRecordService.updateById(tbBaseCheckRecord);
		 return Result.OK("删除成功!");
	 }

	 /**
	  *  批量逻辑删除
	  *
	  * @param ids
	  * @return
	  */
	 @AutoLog(value = "检查计划-批量逻辑删除")
	 @Operation(summary="检查计划-批量逻辑删除")
	 @DeleteMapping(value = "/deleteLogicBatchTbBaseCheckRecord")
	 public Result<?> deleteLogicBatchTbBaseCheckRecord(@RequestParam(name="ids",required=true) String ids) {
		 List<TbBaseCheckRecord> list = tbBaseCheckRecordService.listByIds(Arrays.asList(ids.split(",")));
		 for (TbBaseCheckRecord tbBaseCheckRecord : list) {
			 tbBaseCheckRecord.setDeleted("1");//禁止状态1是已删除，0是正常未删除
		 }
		 tbBaseCheckRecordService.updateBatchById(list);
		 return Result.OK("批量删除成功！");
	 }

	 /**
	  * 获取被逻辑删除的检查计划
	  *
	  * @return logicDeletedUserList
	  */
	 @GetMapping("/recycleBinTbBaseCheckRecord")
	 public Result recycleBinTbBaseCheckRecord(@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
											   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {
		 QueryWrapper<TbBaseCheckRecord> queryWrapper = new QueryWrapper<>();
		 queryWrapper.eq("deleted", "1");//查询已经删除的数据
		 Page<TbBaseCheckRecord> page = new Page<TbBaseCheckRecord>(pageNo, pageSize);
		 IPage<TbBaseCheckRecord> pageList = tbBaseCheckRecordService.page(page, queryWrapper);
		 return Result.ok(pageList);
	 }

	 /**
	  * 还原被逻辑删除的检查计划
	  *
	  * @param jsonObject
	  * @return
	  */
	 @RequestMapping(value = "/putRecycleBinTbBaseCheckRecord", method = RequestMethod.PUT)
	 public Result putRecycleBinTbBaseCheckRecord(@RequestBody JSONObject jsonObject, HttpServletRequest request) {
		 String tbBaseCheckRecordIds = jsonObject.getString("tbBaseCheckRecordIds");
		 if (StringUtils.isNotBlank(tbBaseCheckRecordIds)) {
			 List<TbBaseCheckRecord> list = tbBaseCheckRecordService.listByIds(Arrays.asList(tbBaseCheckRecordIds.split(",")));
			 for (TbBaseCheckRecord entity : list) {
				 entity.setDeleted("0");
			 }
			 tbBaseCheckRecordService.updateBatchById(list);
		 }
		 return Result.ok("还原成功");
	 }
    /**
     * 导出
     * @return
     */
    @RequestMapping(value = "/exportTbBaseCheckRecord")
    public ModelAndView exportTbBaseCheckRecord(HttpServletRequest request, TbBaseCheckRecord tbBaseCheckRecord) {
		 // Step.1 组装查询条件
		 QueryWrapper<TbBaseCheckRecord> queryWrapper = QueryGenerator.initQueryWrapper(tbBaseCheckRecord, request.getParameterMap());
		 LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

		 // Step.2 获取导出数据
		 List<TbBaseCheckRecord> pageList = tbBaseCheckRecordService.list(queryWrapper);
		 List<TbBaseCheckRecord> exportList = null;

		 // 过滤选中数据
		 String selections = request.getParameter("selections");
		 if (oConvertUtils.isNotEmpty(selections)) {
			 List<String> selectionList = Arrays.asList(selections.split(","));
			 exportList = pageList.stream().filter(item -> selectionList.contains(item.getId())).collect(Collectors.toList());
		 } else {
			 exportList = pageList;
		 }

		 // Step.3 AutoPoi 导出Excel
		 ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
		 mv.addObject(NormalExcelConstants.FILE_NAME, "检查情况记录"); //此处设置的filename无效 ,前端会重更新设置一下
		 mv.addObject(NormalExcelConstants.CLASS, TbBaseCheckRecord.class);
		 mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("检查情况记录报表", "导出人:" + sysUser.getRealname(), "检查情况记录"));
		 mv.addObject(NormalExcelConstants.DATA_LIST, exportList);
		 return mv;
    }

    /**
     * 导入
     * @return
     */
    @RequestMapping(value = "/importTbBaseCheckRecord/{mainId}")
    public Result<?> importTbBaseCheckRecord(HttpServletRequest request, HttpServletResponse response, @PathVariable("mainId") String mainId) {
		 MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		 Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
		 for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
			 MultipartFile file = entity.getValue();// 获取上传文件对象
			 ImportParams params = new ImportParams();
			 params.setTitleRows(2);
			 params.setHeadRows(1);
			 params.setNeedSave(true);
			 try {
				 List<TbBaseCheckRecord> list = ExcelImportUtil.importExcel(file.getInputStream(), TbBaseCheckRecord.class, params);
				 for (TbBaseCheckRecord temp : list) {
                    temp.setPlanId(mainId);
				 }
				 long start = System.currentTimeMillis();
				 tbBaseCheckRecordService.saveBatch(list);
				 log.info("消耗时间" + (System.currentTimeMillis() - start) + "毫秒");
				 return Result.OK("文件导入成功！数据行数：" + list.size());
			 } catch (Exception e) {
				 log.error(e.getMessage(), e);
				 return Result.error("文件导入失败:" + e.getMessage());
			 } finally {
				 try {
					 file.getInputStream().close();
				 } catch (IOException e) {
					 e.printStackTrace();
				 }
			 }
		 }
		 return Result.error("文件导入失败！");
    }

    /*--------------------------------子表处理-检查情况记录-end----------------------------------------------*/

    /*--------------------------------子表处理-审批记录-begin----------------------------------------------*/
	/**
	 * 通过主表ID查询
	 * @return
	 */
	@AutoLog(value = "审批记录-通过主表ID查询")
	@Operation(summary="审批记录-通过主表ID查询")
	@GetMapping(value = "/listTbBaseCheckPlanApprovalRecordByMainId")
    public Result<?> listTbBaseCheckPlanApprovalRecordByMainId(TbBaseCheckPlanApprovalRecord tbBaseCheckPlanApprovalRecord,
															   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
															   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
															   HttpServletRequest req) {
        QueryWrapper<TbBaseCheckPlanApprovalRecord> queryWrapper = QueryGenerator.initQueryWrapper(tbBaseCheckPlanApprovalRecord, req.getParameterMap());
        Page<TbBaseCheckPlanApprovalRecord> page = new Page<TbBaseCheckPlanApprovalRecord>(pageNo, pageSize);
        IPage<TbBaseCheckPlanApprovalRecord> pageList = tbBaseCheckPlanApprovalRecordService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

	/**
	 * 添加
	 * @param tbBaseCheckPlanApprovalRecord
	 * @return
	 */
	@AutoLog(value = "审批记录-添加")
	@Operation(summary="审批记录-添加")
	@PostMapping(value = "/addTbBaseCheckPlanApprovalRecord")
	public Result<?> addTbBaseCheckPlanApprovalRecord(@RequestBody TbBaseCheckPlanApprovalRecord tbBaseCheckPlanApprovalRecord) {
		tbBaseCheckPlanApprovalRecordService.save(tbBaseCheckPlanApprovalRecord);
		return Result.OK("添加成功！");
	}

    /**
	 * 编辑
	 * @param tbBaseCheckPlanApprovalRecord
	 * @return
	 */
	@AutoLog(value = "审批记录-编辑")
	@Operation(summary="审批记录-编辑")
	@PutMapping(value = "/editTbBaseCheckPlanApprovalRecord")
	public Result<?> editTbBaseCheckPlanApprovalRecord(@RequestBody TbBaseCheckPlanApprovalRecord tbBaseCheckPlanApprovalRecord) {
		tbBaseCheckPlanApprovalRecordService.updateById(tbBaseCheckPlanApprovalRecord);
		return Result.OK("编辑成功!");
	}

	/**
	 * 通过id删除
	 * @param id
	 * @return
	 */
	@AutoLog(value = "审批记录-通过id删除")
	@Operation(summary="审批记录-通过id删除")
	@DeleteMapping(value = "/deleteTbBaseCheckPlanApprovalRecord")
	public Result<?> deleteTbBaseCheckPlanApprovalRecord(@RequestParam(name="id",required=true) String id) {
		tbBaseCheckPlanApprovalRecordService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 * 批量删除
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "审批记录-批量删除")
	@Operation(summary="审批记录-批量删除")
	@DeleteMapping(value = "/deleteBatchTbBaseCheckPlanApprovalRecord")
	public Result<?> deleteBatchTbBaseCheckPlanApprovalRecord(@RequestParam(name="ids",required=true) String ids) {
	    this.tbBaseCheckPlanApprovalRecordService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
    /**
     * 导出
     * @return
     */
    @RequestMapping(value = "/exportTbBaseCheckPlanApprovalRecord")
    public ModelAndView exportTbBaseCheckPlanApprovalRecord(HttpServletRequest request, TbBaseCheckPlanApprovalRecord tbBaseCheckPlanApprovalRecord) {
		 // Step.1 组装查询条件
		 QueryWrapper<TbBaseCheckPlanApprovalRecord> queryWrapper = QueryGenerator.initQueryWrapper(tbBaseCheckPlanApprovalRecord, request.getParameterMap());
		 LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

		 // Step.2 获取导出数据
		 List<TbBaseCheckPlanApprovalRecord> pageList = tbBaseCheckPlanApprovalRecordService.list(queryWrapper);
		 List<TbBaseCheckPlanApprovalRecord> exportList = null;

		 // 过滤选中数据
		 String selections = request.getParameter("selections");
		 if (oConvertUtils.isNotEmpty(selections)) {
			 List<String> selectionList = Arrays.asList(selections.split(","));
			 exportList = pageList.stream().filter(item -> selectionList.contains(item.getId())).collect(Collectors.toList());
		 } else {
			 exportList = pageList;
		 }

		 // Step.3 AutoPoi 导出Excel
		 ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
		 mv.addObject(NormalExcelConstants.FILE_NAME, "审批记录"); //此处设置的filename无效 ,前端会重更新设置一下
		 mv.addObject(NormalExcelConstants.CLASS, TbBaseCheckPlanApprovalRecord.class);
		 mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("审批记录报表", "导出人:" + sysUser.getRealname(), "审批记录"));
		 mv.addObject(NormalExcelConstants.DATA_LIST, exportList);
		 return mv;
    }

    /**
     * 导入
     * @return
     */
    @RequestMapping(value = "/importTbBaseCheckPlanApprovalRecord/{mainId}")
    public Result<?> importTbBaseCheckPlanApprovalRecord(HttpServletRequest request, HttpServletResponse response, @PathVariable("mainId") String mainId) {
		 MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		 Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
		 for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
			 MultipartFile file = entity.getValue();// 获取上传文件对象
			 ImportParams params = new ImportParams();
			 params.setTitleRows(2);
			 params.setHeadRows(1);
			 params.setNeedSave(true);
			 try {
				 List<TbBaseCheckPlanApprovalRecord> list = ExcelImportUtil.importExcel(file.getInputStream(), TbBaseCheckPlanApprovalRecord.class, params);
				 for (TbBaseCheckPlanApprovalRecord temp : list) {
                    temp.setPlanId(mainId);
				 }
				 long start = System.currentTimeMillis();
				 tbBaseCheckPlanApprovalRecordService.saveBatch(list);
				 log.info("消耗时间" + (System.currentTimeMillis() - start) + "毫秒");
				 return Result.OK("文件导入成功！数据行数：" + list.size());
			 } catch (Exception e) {
				 log.error(e.getMessage(), e);
				 return Result.error("文件导入失败:" + e.getMessage());
			 } finally {
				 try {
					 file.getInputStream().close();
				 } catch (IOException e) {
					 e.printStackTrace();
				 }
			 }
		 }
		 return Result.error("文件导入失败！");
    }

    /*--------------------------------子表处理-审批记录-end----------------------------------------------*/

	 /**
	  *  审批状态修改
	  * @param tbBaseCheckPlan
	  * @return
	  */
	 @AutoLog(value = "检查计划-审批状态修改")
	 @Operation(summary="检查计划-审批状态修改")
	 @PostMapping(value = "/editBpmStatus")
	 public Result<?> editBpmStatus(@RequestBody TbBaseCheckPlanPage tbBaseCheckPlan) {
		 LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		 UpdateWrapper<TbBaseCheckPlan> updateWrapper = new UpdateWrapper<>();
		 updateWrapper.eq("id",tbBaseCheckPlan.getId());
		 updateWrapper.set("bpm_status",tbBaseCheckPlan.getBpmStatus());
		 updateWrapper.set("approver",tbBaseCheckPlan.getApprover());
		 updateWrapper.set("examiner",tbBaseCheckPlan.getExaminer());
		 tbBaseCheckPlanService.update(updateWrapper);

		 if("baseCheckBpmType02".equals(tbBaseCheckPlan.getBpmStatus())){
			 TbBaseCheckPlanApprovalRecord approvalRecord = new TbBaseCheckPlanApprovalRecord();
			 approvalRecord.setPlanId(tbBaseCheckPlan.getId());
			 approvalRecord.setHandlingSuggestion(tbBaseCheckPlan.getHandlingSuggestion());
			 approvalRecord.setProcessor(sysUser.getUsername());
			 approvalRecord.setControls("检查计划-修改审批状态");
			 approvalRecord.setProcessingResult("审批通过");
			 approvalRecord.setOperatingTime(DateUtils.now());
			 tbBaseCheckPlanApprovalRecordService.save(approvalRecord);
		 }
		 return Result.OK("审批状态修改成功!");
	 }

	 /**
	  *  审批变更人员
	  * @param tbBaseCheckPlan
	  * @return
	  */
	 @AutoLog(value = "检查计划-审批变更人员")
	 @Operation(summary="检查计划-审批变更人员")
	 @PostMapping(value = "/editAlterApprover")
	 public Result<?> editAlterApprover(@RequestBody TbBaseCheckPlanPage tbBaseCheckPlan) {
		 LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		 UpdateWrapper<TbBaseCheckPlan> updateWrapper = new UpdateWrapper<>();
		 updateWrapper.eq("id",tbBaseCheckPlan.getId());
		 updateWrapper.set("approver",tbBaseCheckPlan.getApprover());
		 tbBaseCheckPlanService.update(updateWrapper);

		 TbBaseCheckPlanApprovalRecord approvalRecord = new TbBaseCheckPlanApprovalRecord();
		 approvalRecord.setPlanId(tbBaseCheckPlan.getId());
		 approvalRecord.setHandlingSuggestion(tbBaseCheckPlan.getHandlingSuggestion());
		 approvalRecord.setProcessor(sysUser.getUsername());
		 approvalRecord.setControls("检查计划-变更审批人员");
		 approvalRecord.setProcessingResult("变更人员审批成功");
		 approvalRecord.setOperatingTime(DateUtils.now());
		 tbBaseCheckPlanApprovalRecordService.save(approvalRecord);
		 return Result.OK("审批变更人员成功!");
	 }

	 /**
	  * 分页列表查询根据审批人模糊查询
	  * @param tbBaseCheckPlan
	  * @param pageNo
	  * @param pageSize
	  * @param req
	  * @return
	  */
	 @AutoLog(value = "检查计划-分页列表查询根据审批人模糊查询")
	 @Operation(summary="检查计划-分页列表查询根据审批人模糊查询")
	 @GetMapping(value = "/listByApprover")
	 public Result<?> queryPageListByApprover(TbBaseCheckPlan tbBaseCheckPlan,
									@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
									@RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
									HttpServletRequest req) {
		 QueryWrapper<TbBaseCheckPlan> queryWrapper = QueryGenerator.initQueryWrapper(tbBaseCheckPlan, req.getParameterMap());
		 Page<TbBaseCheckPlan> page = new Page<>(pageNo, pageSize);
		 LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		 queryWrapper.like("approver",sysUser.getUsername());
		 queryWrapper.eq("deleted","0");
		 IPage<TbBaseCheckPlan> pageList = tbBaseCheckPlanService.page(page, queryWrapper);
		 return Result.OK(pageList);
	 }

	 /**
	  *  审批状态修改
	  * @param tbBaseCheckPlan
	  * @return
	  */
	 @AutoLog(value = "检查计划-计划状态修改")
	 @Operation(summary="检查计划-计划状态修改")
	 @PostMapping(value = "/editStatus")
	 public Result<?> editStatus(@RequestBody TbBaseCheckPlanPage tbBaseCheckPlan) {
		 LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		 QueryWrapper<TbBaseCheckRecord> queryWrapper = new QueryWrapper<>();
		 queryWrapper.eq("plan_id",tbBaseCheckPlan.getId());
		 queryWrapper.isNull(true,"check_date");
		 List<TbBaseCheckRecord> recordList = tbBaseCheckRecordService.list(queryWrapper);
		if (!recordList.isEmpty()){
			return Result.ok("该检查计划还有未完成的检查项，请完成后检查项后再确认完成任务!");
		}
		 UpdateWrapper<TbBaseCheckPlan> updateWrapper = new UpdateWrapper<>();
		 updateWrapper.eq("id",tbBaseCheckPlan.getId());
		 updateWrapper.set("status",tbBaseCheckPlan.getStatus());
		 boolean flag = tbBaseCheckPlanService.update(updateWrapper);
		 if (flag) {
			 return Result.OK("检查计划-计划状态修改成功!");
		 }else {
			 return Result.error("检查计划-计划状态修改失败!");
		 }
	 }
}
